﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using DICE_GS;

public class MapListManager : LTEventListenerC {
	List<CMapData> mapList;
	List<MapButton> buttonList = new List<MapButton>();
	public static int selectedIndex = -1;
	public static int prevSelectedIndex = -1;
	public static int selectedSingleStageIndex = -1;
	public static int selectedSingleThemeIndex = -1;
	public static string selectedSingleStageMapName = "";
	
	public UIButton StartGameButton;
	public UILabel[] StartGameButtonLabels;
	
	public UIButton enterToRoomButton;
	public UILabel[] enterToRoomButtonLabels;
	
	public UIButton DirectEnterRoombutton;

	// Use this for initialization
	void Start () {
		int index = 0;
		mapList = CMapDataManager.Instance.GetMapList();

		for(int i=0; i<mapList.Count; i++)
		{
            //if( mapList[i].map_type == 0 )
            //{
            //    MapButton button = LTPrefabManager.InstantiateC(PrefabTypes.MapButton, Vector3.one, Quaternion.identity) as MapButton;
            //    button.transform.parent = transform;
            //    button.transform.localScale = Vector3.one;
            //    button.mapButtonImage.spriteName = "UI_map0"+Random.Range(1,4);
            //    button.mapName.text = CStringTableManager.Instance.GetItemByName(mapList[i].title_string);
            //    button.index = index++;
            //    button.manager = this;

            //    buttonList.Add(button);
            //}
		}

		GetComponent<UIGrid>().Reposition();
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	public void SelectMultiMapTap()
	{
		prevSelectedIndex = selectedIndex = -1;
		int index = 0;

		foreach(MapButton button in buttonList)
		{
			Destroy(button.GetComponent<TweenPosition>());
			button.transform.localPosition = Vector3.one;
			button.mapButtonImage.alpha = 1.0f;
			button.SelectedCursor.gameObject.SetActive(false);
			button.singleStageListObject.transform.localScale = new Vector3(1f, 0f, 1f);
			button.isShownSigleStageList = false;
			button.gameObject.SetActive(false);
		}

		for(int i=0; i<mapList.Count; i++)
		{
            //if( mapList[i].map_type == 0 )
            //{
            //    if( buttonList[index] == null )
            //        {
            //        MapButton button = LTPrefabManager.InstantiateC(PrefabTypes.MapButton, Vector3.one, Quaternion.identity) as MapButton;
            //        button.transform.parent = transform;
            //        button.transform.localScale = Vector3.one;
            //        button.mapButtonImage.spriteName = "UI_map0"+Random.Range(1,4);
            //        button.mapName.text = CStringTableManager.Instance.GetItemByName(mapList[i].title_string);
            //        button.index = index;
            //        button.manager = this;

            //        buttonList.Add(button);
            //    }
            //    else
            //    {
            //        buttonList[index].transform.parent = transform;
            //        buttonList[index].transform.localScale = Vector3.one;
            //        buttonList[index].mapButtonImage.spriteName = "UI_map0"+Random.Range(1,4);
            //        buttonList[index].mapName.text = CStringTableManager.Instance.GetItemByName(mapList[i].title_string);
            //        buttonList[index].index = index;
            //        buttonList[index].manager = this;
            //        buttonList[index].gameObject.SetActive(true);
            //    }
            //    index++;
            //}
		}

		GetComponent<UIGrid>().Reposition();

		Transform parentTans = transform.parent;
		parentTans.GetComponent<UIScrollView>().ResetPosition();

		Vector3 localPos = parentTans.localPosition;
		localPos.y = 0f;
		parentTans.localPosition = localPos;
		Vector2 range = parentTans.GetComponent<UIPanel>().clipOffset;
		range.x = 4f;
		range.y = -50f;
		parentTans.GetComponent<UIPanel>().clipOffset = range;
		
		enterToRoomButton.isEnabled = false;
		enterToRoomButton.GetComponentInChildren<UISprite>().spriteName = "UI_btn01_grey";
		DirectEnterRoombutton.isEnabled = false;
		DirectEnterRoombutton.GetComponentInChildren<UISprite>().spriteName = "UI_btn01_grey";
//		foreach(UILabel label in enterToRoomButtonLabels)
//			label.effectColor = new Color(94f, 102f,78f);
	}

	public void SelectSingleMapTap()
	{
		prevSelectedIndex = selectedIndex = -1;
		int index = 0;

		foreach(MapButton button in buttonList)
		{
			Destroy(button.GetComponent<TweenPosition>());
			button.transform.localPosition = Vector3.one;
			button.mapButtonImage.alpha = 1.0f;
			button.SelectedCursor.gameObject.SetActive(false);
			button.gameObject.SetActive(false);
		}

		for(int i=0; i<mapList.Count; i++)
		{
            //if( mapList[i].map_type == 1 )
            //{
            //    if( buttonList[index] == null )
            //    {
            //        MapButton button = LTPrefabManager.InstantiateC(PrefabTypes.MapButton, Vector3.one, Quaternion.identity) as MapButton;
            //        button.transform.parent = transform;
            //        button.transform.localScale = Vector3.one;
            //        button.mapButtonImage.spriteName = "UI_map0"+mapList[i].theme_code;
            //        button.mapName.text = CStringTableManager.Instance.GetItemByName(mapList[i].theme_string);
            //        button.index = index; 
            //        button.manager = this;
					
            //        buttonList.Add(button);
            //    }
            //    else
            //    {
            //        if( mapList[i].theme_code == index+1)
            //        {
            //            buttonList[index].transform.parent = transform;
            //            buttonList[index].transform.localScale = Vector3.one;
            //            buttonList[index].mapButtonImage.spriteName = "UI_map0"+mapList[i].theme_code;
            //            buttonList[index].mapName.text = CStringTableManager.Instance.GetItemByName(mapList[i].theme_string);
            //            buttonList[index].index = index;
            //            buttonList[index].manager = this;
            //            buttonList[index].gameObject.SetActive(true);

            //            for(int j=0; j<5; j++)
            //            {
            //                if( i+j < mapList.Count )
            //                {
            //                    buttonList[index].singleStageNames[j].text = CStringTableManager.Instance.GetItemByName(mapList[i+j].title_string);
            //                    buttonList[index].singleMapNames[j] = mapList[i+j].name;
            //                }
            //                else
            //                {
            //                    buttonList[index].singleStageNames[j].text = "";
            //                }
            //                buttonList[index].singleStageNames[j].transform.parent.GetComponent<LTButtonMessage>().m_MethodDeliver[0].m_EventListener = this;
            //            }

            //            index++;
            //        }
            //        else
            //        {
            //            continue;
            //        }
            //    }
            //}
		}

		GetComponent<UIGrid>().Reposition();

		Transform parentTans = transform.parent;
		parentTans.GetComponent<UIScrollView>().ResetPosition();

		Vector3 localPos = parentTans.localPosition;
		localPos.y = 0f;
		parentTans.localPosition = localPos;
		Vector2 range = parentTans.GetComponent<UIPanel>().clipOffset;
		range.x = 4f;
		range.y = -50f;
		parentTans.GetComponent<UIPanel>().clipOffset = range;
		
		StartGameButton.isEnabled = false;
		StartGameButton.GetComponentInChildren<UISprite>().spriteName = "UI_btn01_grey";
//		foreach(UILabel label in enterToRoomButtonLabels)
//			label.effectColor = new Color(94f, 102f,78f);
	}

	public void SelectButton(int index)
	{
		prevSelectedIndex = selectedIndex;
		selectedIndex = index;
		selectedSingleThemeIndex = selectedIndex - 1;
		
		if( selectedIndex == prevSelectedIndex )
		{
			if( enterToRoomButton.gameObject.activeSelf )
			{
				enterToRoomButton.isEnabled = false;
				enterToRoomButton.GetComponentInChildren<UISprite>().spriteName = "UI_btn01_grey";
				DirectEnterRoombutton.isEnabled = false;
				DirectEnterRoombutton.GetComponentInChildren<UISprite>().spriteName = "UI_btn01_grey";
			}
			else if( StartGameButton.gameObject.activeSelf )
			{
				StartGameButton.isEnabled = false;
				StartGameButton.GetComponentInChildren<UISprite>().spriteName = "UI_btn01_grey";
			}
//			foreach(UILabel label in enterToRoomButtonLabels)
//				label.effectColor = new Color(94f, 102f,78f);
		}
		
		if( !CSceneManager.isSingleGame )
		{
			if( selectedIndex != prevSelectedIndex )
			{
				enterToRoomButton.isEnabled = true;
				enterToRoomButton.GetComponentInChildren<UISprite>().spriteName = "UI_btn01";
				DirectEnterRoombutton.isEnabled = true;
				DirectEnterRoombutton.GetComponentInChildren<UISprite>().spriteName = "UI_btn01";
//				foreach(UILabel label in enterToRoomButtonLabels)
//					label.effectColor = new Color(129f, 169f,42f);
			}
		}
		else
		{
			StartGameButton.isEnabled = false;
			StartGameButton.GetComponentInChildren<UISprite>().spriteName = "UI_btn01_grey";
//			foreach(UILabel label in enterToRoomButtonLabels)
//				label.effectColor = new Color(94f, 102f,78f);
		}

		for(int i=0; i<buttonList.Count; i++)
		{
			if( i != index )
			{

				if( !CSceneManager.isSingleGame )
				{
					buttonList[i].SelectedCursor.gameObject.SetActive(false);
				}
				else
				{
					if( i == prevSelectedIndex && buttonList[i].isShownSigleStageList)
					{
						buttonList[i].HideSingleStages();
						for(int j=i+1; j<buttonList.Count; j++)
						{
							TweenPosition.Begin(buttonList[j].gameObject, 0.3f, new Vector3(buttonList[j].transform.localPosition.x, buttonList[j].transform.localPosition.y+225f, buttonList[j].transform.localPosition.z));
						}
					}
				}
				
				if( prevSelectedIndex != selectedIndex )
				{
					buttonList[i].mapButtonImage.alpha = 0.5f;
				}
				else
				{
					buttonList[i].mapButtonImage.alpha = 1.0f;
				}
			}
			else
			{
				buttonList[i].mapButtonImage.alpha = 1.0f;
				if( !CSceneManager.isSingleGame )
				{
					if( selectedIndex != prevSelectedIndex )
					{
						buttonList[i].SelectedCursor.gameObject.SetActive(true);
					}
					else
					{
						buttonList[i].SelectedCursor.gameObject.SetActive(false);
					}
				}
				else
				{
					if( selectedIndex != prevSelectedIndex )
					{
						buttonList[i].ShowSingleStages();
						for(int j=i+1; j<buttonList.Count; j++)
						{
							TweenPosition.Begin(buttonList[j].gameObject, 0.3f, new Vector3(buttonList[j].transform.localPosition.x, buttonList[j].transform.localPosition.y-225f, buttonList[j].transform.localPosition.z));
						}
						foreach(UISprite sprite in buttonList[i].singleStageSelectedCursors)
						{
							sprite.gameObject.SetActive(false);
						}
					}
					else
					{
						if( buttonList[i].isShownSigleStageList )
						{
							buttonList[i].HideSingleStages();
							for(int j=i+1; j<buttonList.Count; j++)
							{
								TweenPosition.Begin(buttonList[j].gameObject, 0.3f, new Vector3(buttonList[j].transform.localPosition.x, buttonList[j].transform.localPosition.y+225f, buttonList[j].transform.localPosition.z));
							}
							foreach(MapButton button in buttonList)
							{
								button.mapButtonImage.alpha = 1.0f;
							}
						}
						else
						{
							buttonList[i].ShowSingleStages();
							for(int j=i+1; j<buttonList.Count; j++)
							{
								TweenPosition.Begin(buttonList[j].gameObject, 0.3f, new Vector3(buttonList[j].transform.localPosition.x, buttonList[j].transform.localPosition.y-225f, buttonList[j].transform.localPosition.z));
							}
							foreach(UISprite sprite in buttonList[i].singleStageSelectedCursors)
							{
								sprite.gameObject.SetActive(false);
							}
						}
					}
				}
			}
		}
		
		if( selectedIndex == prevSelectedIndex )
		{
			selectedIndex = -1;
		}
	}

	public override void OnEvent (int eventID)
	{
		for(int i=0; i<5; i++)
		{
			if( i == eventID-1 )
			{
				buttonList[selectedIndex].singleStageSelectedCursors[i].gameObject.SetActive(true);
//				buttonList[selectedIndex].singleStageNames[i].color = new Color(255f, 255f, 255f);
			}
			else
			{
				buttonList[selectedIndex].singleStageSelectedCursors[i].gameObject.SetActive(false);
//				buttonList[selectedIndex].singleStageNames[i].color = new Color(214f, 199f, 135f);
			}
		}
		selectedSingleStageMapName = buttonList[selectedIndex].singleMapNames[eventID-1];
		
		StartGameButton.isEnabled = true;
		StartGameButton.GetComponentInChildren<UISprite>().spriteName = "UI_btn01";
//		foreach(UILabel label in enterToRoomButtonLabels)
//			label.effectColor = new Color(129f, 169f,42f);
	}
}
